package com.ainiyar.springweb.controller.admin;

import com.ainiyar.springweb.common.result.ResultResponse;
import com.ainiyar.springweb.service.ConfigsService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

/**************************************
 * File Name : ConfigsController
 * Created with : IntelliJ IDEA.
 * Author : 艾尼亚尔·毛拉吾提
 * QQ : 524155792  WeChat : ainiyar0124
 * Created Time : 2024/2/18  18:40
 * Desc :
 ***************************************/
@RestController
public class ConfigsController {
    @Resource
    private ConfigsService configsService;

    /**
     * 获取系统配置
     */
    @GetMapping("/get_config")
    public ResultResponse<Object> getConfig(){
        return ResultResponse.Result(configsService.getConfig());
    }

    @GetMapping("/conf/get_settings")
    @PreAuthorize("hasAuthority('setting:list')")
    public ResultResponse<Object> getSettings() {
        return ResultResponse.Result(configsService.getSettings());
    }

    @PostMapping("/conf/save_basic")
    @PreAuthorize("hasAuthority('setting:basic')")
    public ResultResponse<String> saveBasic(HttpServletRequest request){
        Map<String, String[]> parameters = request.getParameterMap();
         boolean save=configsService.saveSettingData(parameters,"basic");
         if (save){
             return ResultResponse.Success("修改基本设置成功");
         }
        return ResultResponse.Fail("修改基本设置失败");
    }
    @PostMapping("/conf/save_policy_a")
    @PreAuthorize("hasAuthority('setting:policy_a')")
    public ResultResponse<String> savePolicyA(HttpServletRequest request){
        Map<String, String[]> parameters = request.getParameterMap();
        boolean save=configsService.saveSettingData(parameters,"policy_a");
        if (save){
            return ResultResponse.Success("修改政策协议成功");
        }
        return ResultResponse.Fail("修改政策协议失败");
    }

    @PostMapping("/conf/save_customer")
    @PreAuthorize("hasAuthority('setting:customer')")
    public ResultResponse<String> saveCustomer(HttpServletRequest request){
        Map<String, String[]> parameters = request.getParameterMap();
        boolean save=configsService.saveSettingData(parameters,"customer");
        if (save){
            return ResultResponse.Success("修改客服设置成功");
        }
        return ResultResponse.Fail("修改客服设置失败");
    }
    @PostMapping("/conf/save_user")
    @PreAuthorize("hasAuthority('setting:user')")
    public ResultResponse<String> saveUser(HttpServletRequest request){
        Map<String, String[]> parameters = request.getParameterMap();
        boolean save=configsService.saveSettingData(parameters,"user");
        if (save){
            return ResultResponse.Success("修改用户设置成功");
        }
        return ResultResponse.Fail("修改用户设置失败");
    }
    @PostMapping("/conf/save_withdrawal")
    @PreAuthorize("hasAuthority('setting:withdrawal')")
    public ResultResponse<String> saveWithdrawal(HttpServletRequest request){
        Map<String, String[]> parameters = request.getParameterMap();
        boolean save=configsService.saveSettingData(parameters,"withdrawal");
        if (save){
            return ResultResponse.Success("修改提现设置成功");
        }
        return ResultResponse.Fail("修改提现设置失败");
    }
    @PostMapping("/conf/save_sms")
    @PreAuthorize("hasAuthority('setting:sms')")
    public ResultResponse<String> saveSMS(HttpServletRequest request){
        Map<String, String[]> parameters = request.getParameterMap();
        boolean save=configsService.saveSettingData(parameters,"sms");
        if (save){
            return ResultResponse.Success("修改短信设置成功");
        }
        return ResultResponse.Fail("修改短信设置失败");
    }

}

